/**
 * 
 */
package com.agrec.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.agrec.client.actors.Position;
import com.agrec.client.services.KMRServiceAsync;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Lo2a
 *
 */
public class OpportunitiesPage extends Composite implements KMRPage {

	private VerticalPanel panel;
	private Label header;
	private FlexTable table;
	private KMRServiceAsync kmrService;
	//private List<Position> positions;
	protected ArrayList<Position> positions;
	
	public OpportunitiesPage(KMRServiceAsync kmrService) {
		this.kmrService = kmrService;
		init();
	}

	private void init() {
		panel = new VerticalPanel();
		header = new Label("Opportunities");
		header.addStyleName("large");
		
		table = new FlexTable();
		
		//table header
		table.setWidget(0, 0, new Label("Job Title"));
		table.setWidget(0, 1, new Label("Location"));
		table.setWidget(0, 2, new Label("Contact"));
		
		//table content
		getRecentOpportunities(10);
		
		
		
		panel.add(header);
		panel.add(table);
		
		initWidget(panel);
		
	}

	private void getRecentOpportunities(int numOfPos) {
		kmrService.getRecentOpportunities(numOfPos, new AsyncCallback<ArrayList<Position>>(){
			
			public void onFailure(Throwable caught) {
				
			}

			public void onSuccess(ArrayList<Position> result) {
				int i = 1;
				for (Iterator<Position> iterator = result.iterator(); iterator.hasNext();) {
					Position pos = iterator.next();
					Hyperlink jobTitleHL = new Hyperlink(pos.jobTitle(), "");
					table.setWidget(i, 0, jobTitleHL);
					table.setWidget(i, 1, new Label(pos.city()));
					table.setWidget(i, 2, new Hyperlink(pos.userName(),""));
					i++;
				}
				//positions = result;
				
			}
		});
		//return positions;
	}

	/* (non-Javadoc)
	 * @see com.agrec.client.KMRPage#historyName()
	 */
	//@Override
	public String historyName() {
		return "OpportunitiesPage";
	}

	/* (non-Javadoc)
	 * @see com.agrec.client.KMRPage#update()
	 */
	//@Override
	public void update() {
		// TODO Auto-generated method stub

	}

}
